package com.hc.contorl;


import com.hc.pojo.TbUser;
import com.hc.service.TbUserService;
import com.hc.service.impl.TbUserServiceImpl;
import com.hc.util.CookieUtils;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName:
 * @Package: com.example.my.shop.mybatis.controller
 * @ClassName: LoginController
 * @Description: 登录
 * @Author: gy
 * @CreateDate: 2019/6/19 12:30
 * @Version: 1.0.0
 */
@WebServlet(value = "/login")
public class LoginContorller extends HttpServlet {
    String cookieName = "UserInfo";
    String userSession = "userSession";

    /**
     * @Method: doGet
     * @Description: 检查游览器是否有Cookie, 有则回显到login.jsp页面
     * @Param: [req, resp]
     * @return: void
     * @Author: gy
     * @Date: 2019/6/19
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String cookieValue = CookieUtils.getCookieValue(req, cookieName);
        if (cookieValue != null && (cookieValue.equals(""))) {
            String[] arr = cookieValue.split(":");
            req.setAttribute("email", arr[0]);
            req.setAttribute("password", arr[1]);
            req.setAttribute("rememberMe", "checked");
        }
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    /**
     * @Method: doPost
     * @Description: 用户登录
     * @Param: [req, resp]
     * @return: void
     * @Author: Mr.Vincent
     * @Date: 2019/5/29
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


        String email = req.getParameter("email");
        String passWord = req.getParameter("password");
        String rememberMe = req.getParameter("rememberMe");

        TbUserService tbUserService = new TbUserServiceImpl();
        TbUser tbUser = tbUserService.selectEmailAndPassWord(email);
        System.out.println(tbUser);
        if (!"on".equals(rememberMe)) {
            CookieUtils.deleteCookie(req, resp, cookieName);
        }
        if (tbUser != null) {
            if (tbUser.getPassword().equals(DigestUtils.md5Hex(passWord))) {
                if ("on".equals(rememberMe)) {
                    CookieUtils.setCookie(req, resp, cookieName, String.format("%s:%s", email, passWord));
                }
                req.getSession().setAttribute(userSession, tbUser);
                //如果账号密码正确，发送重定向到main.jsp
                resp.sendRedirect("main.jsp");
            } else {
                //保存属性
                req.setAttribute("message", "密码错误，请重新登录");
                //请求转发
                req.getRequestDispatcher("login.jsp").forward(req, resp);

            }
        } else {
            req.setAttribute("message", "账号不存在");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }
}






